package io.chenglicun.modules.data.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.service.impl.BaseServiceImpl;
import io.chenglicun.modules.data.dao.DataGridVillageDao;
import io.chenglicun.modules.data.entity.DataGridVillageEntity;
import io.chenglicun.modules.data.enums.DataGridVillageTypeEnums;
import io.chenglicun.modules.data.service.DataGridVillageService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Excel导入演示
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
public class DataGridVillageServiceImpl extends BaseServiceImpl<DataGridVillageDao, DataGridVillageEntity> implements DataGridVillageService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public boolean excelData(List<List<Object>> list) {
        List<DataGridVillageEntity> insertList = new ArrayList<>();
        for (List<Object> o: list) {
            try {
                Integer parentId = 0;
                DataGridVillageEntity dataGridVillageEntity = new DataGridVillageEntity();
                String parentName = o.get(1) == null ? "0" : o.get(1).toString();
                if (!parentName.equals("0")){
                    DataGridVillageEntity queryDataGridVillageEntity = new DataGridVillageEntity();
                    queryDataGridVillageEntity.setType(DataGridVillageTypeEnums.GRID.getValue());
                    queryDataGridVillageEntity.setName(parentName);
                    DataGridVillageEntity parentDataGridVillageEntity = baseDao.queryDataGrid(queryDataGridVillageEntity);
                    if (parentDataGridVillageEntity !=null){
                        parentId= parentDataGridVillageEntity.getId();
                    }
                }
                dataGridVillageEntity.setName(o.get(0)==null?"":o.get(0).toString());
                dataGridVillageEntity.setStatus(1);
                dataGridVillageEntity.setType(DataGridVillageTypeEnums.GRID.getValue());
                dataGridVillageEntity.setParentId(parentId);
                dataGridVillageEntity.setCreator(SecurityUser.getUserId());
                dataGridVillageEntity.setCreateDate(new Date());
                dataGridVillageEntity.setUpdater(SecurityUser.getUserId());
                dataGridVillageEntity.setUpdaterDate(new Date());
                if (parentId==0){
                    super.insert(dataGridVillageEntity);
                }else {
                    insertList.add(dataGridVillageEntity);
                }
            }catch (Exception e){
                logger.info("插入失败："+ JSONUtil.toJsonStr(o));
            }

        }
        super.insertBatch(insertList);
        return true;
    }

    @Override
    public PageData<DataGridVillageEntity> page(Map<String, Object> params) {
        IPage<DataGridVillageEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, DataGridVillageEntity.class);
    }


    public void update(DataGridVillageEntity entity) {
        entity.setUpdater(SecurityUser.getUserId());
        entity.setUpdaterDate(new Date());
        super.updateById(entity);
    }

    public void delete(Integer[] ids) {
        super.deleteBatchIds(Arrays.asList(ids));
    }
    public void save(DataGridVillageEntity entity) {
        entity.setCreator(SecurityUser.getUserId());
        entity.setCreateDate(new Date());
        super.insert(entity);
    }

    @Override
    public boolean excelVillage(List<List<Object>> list) {
        List<DataGridVillageEntity> insertList = new ArrayList<>();
        for (List<Object> o: list) {
            try {
                Integer parentId = 0;
                DataGridVillageEntity dataGridVillageEntity = new DataGridVillageEntity();
                String parentName = o.get(1) == null ? "0" : o.get(1).toString();
                if (!parentName.equals("0")){
                    DataGridVillageEntity queryDataGridVillageEntity = new DataGridVillageEntity();
                    queryDataGridVillageEntity.setType(DataGridVillageTypeEnums.GRID.getValue());
                    queryDataGridVillageEntity.setName(parentName);
                    DataGridVillageEntity parentDataGridVillageEntity = baseDao.queryDataGrid(queryDataGridVillageEntity);
                    if (parentDataGridVillageEntity !=null){
                        parentId= parentDataGridVillageEntity.getId();
                    }
                }
                dataGridVillageEntity.setName(o.get(0)==null?"":o.get(0).toString());
                dataGridVillageEntity.setStatus(1);
                dataGridVillageEntity.setType(DataGridVillageTypeEnums.VILLAGE.getValue());
                dataGridVillageEntity.setParentId(parentId);
                dataGridVillageEntity.setCreator(SecurityUser.getUserId());
                dataGridVillageEntity.setCreateDate(new Date());
                dataGridVillageEntity.setUpdater(SecurityUser.getUserId());
                dataGridVillageEntity.setUpdaterDate(new Date());
                if (parentId==0){
                    super.insert(dataGridVillageEntity);
                }else {
                    insertList.add(dataGridVillageEntity);
                }
            }catch (Exception e){
                logger.info("插入失败："+ JSONUtil.toJsonStr(o));
            }

        }
        super.insertBatch(insertList);
        return true;
    }

    public DataGridVillageEntity queryDataGrid(String name){
        DataGridVillageEntity queryDataGridVillageEntity = new DataGridVillageEntity();
        queryDataGridVillageEntity.setName(name);
        DataGridVillageEntity dataGridVillageEntity = baseDao.queryDataGrid(queryDataGridVillageEntity);

        return dataGridVillageEntity;
    }

    public QueryWrapper<DataGridVillageEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<DataGridVillageEntity> wrapper = new QueryWrapper<>();

        String name = (String)params.get("name");
        wrapper.eq(StringUtils.isNotBlank(name), "name", name);
        String type = (String)params.get("type");
        wrapper.eq(StringUtils.isNotBlank(type), "type", type);
        wrapper.orderByDesc("create_date");
        return wrapper;
    }
}